// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    // 自动关闭警告消息
    setTimeout(function() {
        const alerts = document.querySelectorAll('.alert');
        alerts.forEach(function(alert) {
            if (typeof bootstrap !== 'undefined') {
                const bsAlert = new bootstrap.Alert(alert);
                bsAlert.close();
            } else {
                alert.style.display = 'none';
            }
        });
    }, 5000);
    
    // 添加滚动动画
    const animateElements = document.querySelectorAll('.card, .section-title, .feature-icon, .stat-item, .testimonial-card, .coach-card, .activity-card');
    
    // 初始隐藏所有元素
    animateElements.forEach(function(element) {
        element.classList.add('animate-on-scroll');
    });
    
    // 显示视口中的元素
    function showVisibleElements() {
        animateElements.forEach(function(element) {
            const elementTop = element.getBoundingClientRect().top;
            const elementBottom = element.getBoundingClientRect().bottom;
            
            // 检查元素是否在视口中
            if (elementTop < window.innerHeight - 100 && elementBottom > 0) {
                element.classList.add('visible');
            }
        });
    }
    
    // 初始检查
    setTimeout(showVisibleElements, 100);
    
    // 滚动时检查
    window.addEventListener('scroll', showVisibleElements);
    
    // 数字计数动画
    const statNumbers = document.querySelectorAll('.stat-number');
    let animated = false;
    
    function animateNumbers() {
        if (animated) return;
        
        const statsSection = document.querySelector('.stats-section');
        if (!statsSection) return;
        
        const statsSectionTop = statsSection.getBoundingClientRect().top;
        
        if (statsSectionTop < window.innerHeight - 100) {
            statNumbers.forEach(function(statNumber) {
                const target = parseInt(statNumber.textContent);
                let count = 0;
                const duration = 2000; // 2秒
                const interval = Math.floor(duration / target);
                
                const counter = setInterval(function() {
                    count += 1;
                    statNumber.textContent = count + (statNumber.textContent.includes('+') ? '+' : '');
                    
                    if (count >= target) {
                        clearInterval(counter);
                    }
                }, interval);
            });
            
            animated = true;
        }
    }
    
    // 滚动时检查是否应该开始数字动画
    window.addEventListener('scroll', animateNumbers);
    
    // 初始检查
    setTimeout(animateNumbers, 100);
}); 